package handler

import (
	"context"
	"encoding/json"
	"time"
	"video_server/basic/global"
	"video_server/internal/model"
	__ "video_server/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddRequest) (*__.VideoAddResponse, error) {
	video := model.Videos{
		Title:             in.Title,
		Author:            in.Author,
		Times:             in.Times,
		Likes:             int(in.Likes),
		Types:             in.Types,
		Img:               in.Img,
		UploadMethod:      in.UploadMethod,
		MemoryAddress:     in.MemoryAddress,
		TranscodingScheme: in.TranscodingScheme,
	}
	global.DB.Debug().Create(&video)

	return &__.VideoAddResponse{Id: int64(video.Id)}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) VideoList(_ context.Context, in *__.VideoListRequest) (*__.VideoListResponse, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize

	key := "video:list"
	if data, err := global.Rdb.Get(context.Background(), key).Bytes(); err == nil {
		var list []*__.VideoList
		json.Unmarshal(data, &list)
		return &__.VideoListResponse{List: list}, nil
	}
	var VideoList []model.Videos
	global.DB.Debug().Offset(int(offset)).Limit(int(pageSize)).Find(&VideoList)
	var list []*__.VideoList
	for _, v := range VideoList {
		list = append(list, &__.VideoList{
			Id:                int64(v.Id),
			Title:             v.Title,
			Author:            v.Author,
			Times:             v.Times,
			Likes:             int64(v.Likes),
			Types:             v.Types,
			Img:               v.Img,
			UploadMethod:      v.UploadMethod,
			MemoryAddress:     v.MemoryAddress,
			TranscodingScheme: v.TranscodingScheme,
		})
	}
	if data, err := json.Marshal(list); err == nil {
		global.Rdb.Set(context.Background(), key, data, time.Minute*5)
	}
	return &__.VideoListResponse{List: list}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) UserLogin(_ context.Context, in *__.UserLoginRequest) (*__.UserLoginResponse, error) {
	var user model.Users1
	global.DB.Debug().Where("phone=?", in.Phone).Find(&user)
	if user.Id == 0 {
		users := model.Users1{
			Img:     in.Img,
			VideoId: uint(in.VideoId),
			Phone:   in.Phone,
			Name:    in.Name,
			Text:    in.Text,
			Address: in.Address,
		}
		global.DB.Debug().Create(&users)
		return &__.UserLoginResponse{Id: int64(users.Id)}, nil
	}
	return &__.UserLoginResponse{Id: int64(user.Id)}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) VideoInfo(_ context.Context, in *__.UserInfoRequest) (*__.UserInfoResponse, error) {
	var user model.Users1
	global.DB.Debug().Where("id=?", in.UserId).Find(&user)
	var VideoList []model.Videos
	global.DB.Debug().Find(&VideoList)
	var list []*__.VideoInfo
	for _, v := range VideoList {
		list = append(list, &__.VideoInfo{
			Id:                int64(v.Id),
			Title:             v.Title,
			Author:            v.Author,
			Times:             v.Times,
			Likes:             int64(v.Likes),
			Types:             v.Types,
			Img:               v.Img,
			UploadMethod:      v.UploadMethod,
			MemoryAddress:     v.MemoryAddress,
			TranscodingScheme: v.TranscodingScheme,
		})
	}
	return &__.UserInfoResponse{List: list}, nil
}
